package com.wangsd.web.shop.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wangsd.common.basicbean.MyController;
import com.wangsd.common.basicbean.Result;
import com.wangsd.common.utils.DateUtils;
import com.wangsd.web.shop.entity.Activity;
import com.wangsd.web.shop.entity.Store;
import com.wangsd.web.shop.service.ActivityDetailService;
import com.wangsd.web.shop.service.ActivityService;
import com.wangsd.web.shop.service.StoreService;
import com.wangsd.web.sys.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Api(tags = "支付商户管理")
@RestController
@RequestMapping("/shop/activity")
@Slf4j
public class ActivityController extends MyController {
    @Resource
    private ActivityService service;
    @Autowired
    ActivityDetailService activityDetailService;

    @ApiOperation(value = "分页查询列表")
    @PostMapping("/selectPage")
    public Object selectPage(@ApiParam(value = "当前第几页", required = true) @RequestParam("pageNum") Integer pageNum,
                             @ApiParam(value = "每页结果集大小", required = true) @RequestParam("pageSize") Integer pageSize,
                             Activity queryObj) {
        SysUser loginUser = getUserInfo();
        Page page = PageHelper.startPage(pageNum, pageSize);
        List<Activity> list = service.selectPage(queryObj, loginUser.getParentCode());
        for (Activity activity : list) {
            if (StringUtils.isNotBlank(activity.getCouponIds())) {
                activity.setIdList(JSON.parseArray(activity.getCouponIds(), Long.class));
            }
            if (activity.getType() == 2) { //大转盘
                activity.setDetailList(activityDetailService.selectByActivityId(activity.getId()));
            }
//            if (activity.getBeginTime() != null && activity.getEndTime() != null) {
//                List<String> validTime = new ArrayList<>();
//                validTime.add(DateUtils.formatDatetime(activity.getBeginTime()));
//                validTime.add(DateUtils.formatDatetime(activity.getEndTime()));
//                activity.setValidTime(validTime);
//            }
        }
        return Result.success(getPageInfo(page, list));
    }

    @ApiOperation(value = "修改")
    @PostMapping("/updateActivity")
    public Object updateActivity(@RequestBody Activity entity) {
        if (entity.getIdList() != null) {
            entity.setCouponIds(JSON.toJSONString(entity.getIdList()));
        }
//        if (entity.getValidTime() != null) {
//            Date beginTime = DateUtils.parseDate(entity.getValidTime().get(0), "yyyy-MM-dd'T'HH:mm:ss");
//            Date endTime = DateUtils.parseDate(entity.getValidTime().get(1), "yyyy-MM-dd'T'HH:mm:ss");
//            entity.setBeginTime(beginTime);
//            entity.setEndTime(endTime);
//        }
        service.updateActivity(entity);
        return Result.success();
    }

    @ApiOperation(value = "删除活动")
    @GetMapping("/{id}/delete")
    public Result delete(@ApiParam(value = "活动id", required = true) @PathVariable Long id) {
        service.deleteById(id);
        return Result.success();
    }


}
